package com.zhang.demo

import android.os.Bundle
import android.os.Debug
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhang.demo.base.BaseActivity
import com.zhang.demo.contract.FileView
import com.zhang.demo.model.entity.FileEntity
import com.zhang.demo.presenter.FilePresenter
import com.zhang.demo.ui.adaper.BaseViewHolder
import com.zhang.demo.ui.adaper.FileAdapter
import com.zhang.demo.ui.adaper.OnItemClickListener
import com.zhang.demo.ui.adaper.OnItemLongClickListener
import kotlinx.android.synthetic.main.activity_main.*
import javax.inject.Inject

class MainActivity : BaseActivity(),FileView {
    @Inject
    lateinit var filePresenter: FilePresenter
    lateinit var fileAdapter: FileAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Debug.startMethodTracing("115trace")
        activityComponent()?.inject(this)
        filePresenter.attachView(this)

        filePresenter.loadData();

        recyclerView.setLayoutManager(LinearLayoutManager(this))
        fileAdapter = FileAdapter(this)
        recyclerView.adapter = fileAdapter
        fileAdapter.setOnItemLongClickListener { holder, targetView, position ->
            val file = holder?.data
            if (file is FileEntity) {
                fileAdapter.removeData(file)
                filePresenter.removeFile(file.fid)
            }
            fileAdapter.notifyDataSetChanged()
            return@setOnItemLongClickListener true
        }

    }

    override fun onResume() {
        super.onResume()
        Debug.stopMethodTracing()
    }
    override fun onDestroy() {
        super.onDestroy()
        filePresenter.detachView()
    }

    override fun refresh(fileList: List<FileEntity>?) {
        fileAdapter.data = fileList
        fileAdapter.notifyDataSetChanged()
    }
}
